home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / BEERSRC.ZIP / EXPL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-27  |  17.3 KB  |  894 lines

  1. #include <io.h>
  2. #include <fcntl.h>
  3. #include <sys\stat.h>
  4. #include <alloc.h>
  5. #include <dos.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdarg.h>
  11.  
  12. #include "xmode.h"
  13. #include "mouse.h"
  14. #define MAIN_MODULE
  15. #include "baller.h"
  16.  
  17.  
  18. #define MIDX                 160
  19. #define MIDY                 110
  20.  
  21. #define STATLINE             210
  22.  
  23.  
  24. void plot(int x, int y, int c);
  25. int getpixel(int x, int y);
  26.  
  27. struct sprstrc *font;
  28.  
  29. int    nsprites;
  30. struct sTableEntry *sprite;
  31. int    sspr = 0;
  32.  
  33.  
  34. int    x, y, b, x0, y0;
  35. int    count;
  36.  
  37. struct explstrc tmpexpl;
  38. int    tmp[1000];
  39. int    tmppos = 0;
  40.  
  41. int    runpos;
  42.  
  43.  
  44. int    nexpls = 0;
  45. #define NEXPLS            80
  46. struct explstrc *bumm[NEXPLS];
  47. int    expllen[NEXPLS];
  48. int    sexpl = -1;
  49.  
  50. int    nfoes = 0;
  51. int    xstart[400], ystart[400];
  52. int    pathlen[400];
  53. struct foestrc *foe[400];
  54. int    sfoe = -1;
  55.  
  56.  
  57. int loadfont(void)
  58. {
  59.    int      filvar;
  60.    long        size;
  61.    int      nentries;
  62.    int      i;
  63.  
  64.    if ((filvar = open("font1.spr", O_RDONLY | O_BINARY, S_IREAD)) == -1) {
  65.       return -1;
  66.    }
  67.    size = filelength(filvar);
  68.  
  69.    if ((font = malloc(size)) == NULL) {
  70.       close(filvar);
  71.       return -1;
  72.    }
  73.    read(filvar, font, size);
  74.    close(filvar);
  75.  
  76.    return 0;
  77.  
  78. }
  79.  
  80.  
  81.  
  82. int readslib(char *file)
  83. {
  84.  
  85.    int      filvar;
  86.    long        size;
  87.    int      i;
  88.    char     huge *h;
  89.  
  90.    if ((filvar = open(file, O_RDONLY | O_BINARY, S_IREAD)) == -1) {
  91.       return -1;
  92.    }
  93.    read(filvar, &nsprites, 2);
  94.    size = filelength(filvar) - 2;
  95.  
  96.    if ((sprite = (void *) farmalloc(size)) == NULL) {
  97.       close(filvar);
  98.       abort();
  99.    }
  100.  
  101.    h = (char *)sprite;
  102.    while (size > 65000) {
  103.       read(filvar, h, 65000);
  104.       h += 65000; size -= 65000;
  105.    }
  106.    read(filvar, h, size);
  107.  
  108.    close(filvar);
  109.  
  110.  
  111. // Relocate pointers to single data elements.
  112.    for (i = 0; i < nsprites; i++) {
  113.       (long)sprite[i].sprite += (long)sprite;
  114.    }
  115.    return 0;
  116.  
  117. }
  118.  
  119.  
  120. void xorline(int x1, int y1, int x2, int y2, int c)
  121. {
  122.  
  123.    int   x, y, z, dx, dy, dz, i1, i2;
  124.  
  125.  
  126.    dx = abs(x1 - x2); dy = abs(y1 - y2);
  127.    if (x1 < x2) {
  128.       x = x1; y = y1;
  129.       if (y1 > y2) z = -1; else z = 1;
  130.    } else {
  131.       x = x2; y = y2;
  132.       if (y2 > y1) z = -1; else z = 1;
  133.    }
  134.  
  135.    if (dx > dy) i2 = dx; else i2 = dy;
  136.    dz = i2 >> 1;
  137.    plot(x, y, getpixel(x, y) ^ c);
  138.    for (i1 = 1; i1 <= i2; i1++) {
  139.       if (dz < dx) {
  140.      dz = dz + dy; x++;
  141.       }
  142.       if (dz >= dx) {
  143.      dz = dz - dx; y += z;
  144.       }
  145.       plot(x, y, getpixel(x, y) ^ c);
  146.    }
  147.  
  148. }
  149.  
  150. int getdec(char *text)
  151. {
  152.    int   i;
  153.    int   ch;
  154.    char  str[40];
  155.    int   pos;
  156.  
  157.    clearregion(STATLINE-8, 8);
  158.    writetext(0, STATLINE-8, text, font);
  159.  
  160.    i = 0;
  161.    pos = strlen(text)*8 + 8;
  162.    while ((ch = getch()) != 13) {
  163.       if (ch == 8) {
  164.      if (i > 0) {
  165.      pos -= 8; putspritedirect(font, pos, STATLINE-8, 0);
  166.      i--;
  167.      }
  168.       } else {
  169.      str[i++] = ch;
  170.      putspritedirect(font, pos, STATLINE-8, ch - ' ');
  171.      pos+=8;
  172.       }
  173.  
  174.    }
  175.    str[i] = 0;
  176.  
  177.    return atoi(str);
  178.  
  179. }
  180.  
  181.  
  182. #pragma argsused
  183. void error(char *s, int code, ...)
  184. {
  185.    writetext(0, 0, s, font);
  186.    getch();
  187.    writetext(0,0,"                                   ", font);
  188.  
  189. }
  190.  
  191.  
  192. int findcommand(void *path, int i, int dir)
  193. {
  194.    unsigned *data;
  195.  
  196.    if (path == NULL) data = (unsigned *)tmp;
  197.    else data = (unsigned *)path;
  198.  
  199.    if (i < 0) return i+dir;
  200.    if (dir == -1) {
  201.       if (i == 0) return -1;
  202.       i--;
  203.       if ((i > 0) && (data[i-1] == EXPLSOUND)) return i-1;
  204.       if ((i > 2) && (data[i-3] == EXPLNEW)) return i-3;
  205.       if ((i > 2) && (data[i-3] == EXPLRELEASEFOE)) return i-3;
  206.       if ((i > 4) && (data[i-5] == EXPLNEWPATH)) return i-5;
  207.       return i;
  208.    } else {
  209.       if (data[i] == EXPLNEW) return i+4;
  210.       if (data[i] == EXPLSOUND) return i+2;
  211.       if (data[i] == EXPLRELEASEFOE) return i+4;
  212.       if (data[i] == EXPLNEWPATH) return i+6;
  213.       return i+1;
  214.    }
  215.  
  216. }
  217.  
  218. int dispatch(int pos, char *s)
  219. {
  220.  
  221.    if (pos < 0) {
  222.       strcpy(s, "NONE");
  223.       return pos+1;
  224.    }
  225.    switch (tmp[pos]) {
  226.       case EXPLEND:
  227.      strcpy(s, "END");
  228.      pos++;
  229.      break;
  230.       case EXPLNEW:
  231.      sprintf(s, "NEW %02d", tmp[pos+1]);
  232.      pos += 4;
  233.      break;
  234.       case EXPLSOUND:
  235.      sprintf(s, "SOUND %02d", tmp[pos+1]);
  236.      pos += 2;
  237.      break;
  238.       case EXPLWAIT:
  239.      sprintf(s, "WAIT");
  240.      pos += 1;
  241.      break;
  242.       case EXPLREMOVEOBJ:
  243.      sprintf(s, "REMOVEOBJ");
  244.      pos++;
  245.      break;
  246.       case EXPLRELEASEFOE:
  247.      sprintf(s, "RELEASE %02d", tmp[pos+1]);
  248.      pos+=4;
  249.      break;
  250.       case EXPLNEWPATH:
  251.      sprintf(s, "NEWPATH %02d", tmp[pos+1]);
  252.      pos += 6;
  253.      break;
  254.       default:
  255.      sprintf(s, "ERR %x", tmp[pos]);
  256.      pos+=1;
  257.      break;
  258.    }
  259.  
  260.    return pos;
  261. }
  262.  
  263. void status(void)
  264. {
  265.    char   text[80];
  266.  
  267.    clearregion(STATLINE-8, 16);
  268.    sprintf(text, "%04X", count);
  269.    writetext(284, STATLINE, text, font);
  270.    sprintf(text, "%d", sexpl);
  271.    writetext(0, STATLINE, text, font);
  272.  
  273.    dispatch(runpos, text);
  274.    writetext(20, STATLINE, text, font);
  275.  
  276. }
  277.  
  278.  
  279.  
  280. int roll(int n, int lb, int ub, int dir)
  281. {
  282.    if (dir == 1) {
  283.       n++; if (n > ub) n = lb;
  284.    } else {
  285.       n--; if (n < lb) n = ub;
  286.    }
  287.  
  288.    return n;
  289. }
  290.  
  291. void insertcommand(int pos, int n, ...)
  292. {
  293.    va_list ap;
  294.    int     i;
  295.  
  296.    if (pos >= tmppos) tmppos = findcommand(NULL, pos, 1);
  297.    memmove(&tmp[pos+n], &tmp[pos], (tmppos-pos)*2);
  298.    tmppos += n;
  299.    va_start(ap, n);
  300.    for (i = 0; i < n; i++) {
  301.       tmp[pos+i] = va_arg(ap, int);
  302.    }
  303.    va_end(ap);
  304.  
  305. }
  306.  
  307. void fillwait(void)
  308. {
  309.    int  i;
  310.  
  311.    for (i = 0; i < 1000; i++)
  312.       tmp[i] = EXPLWAIT;
  313.  
  314. }
  315.  
  316. void newexpl(void)
  317. {
  318.  
  319.    clearscreen();
  320.  
  321.    fillwait();
  322.    tmpexpl = *bumm[sexpl];
  323.  
  324.    runpos = -1;
  325.  
  326.    tmppos = expllen[sexpl]-sizeof(struct explstrc);
  327.    memcpy(tmp, &bumm[sexpl]->data, tmppos);
  328.    tmppos = tmppos/2;
  329.    tmp[tmppos-1] = EXPLWAIT;
  330.  
  331. }
  332.  
  333. void storepath(void)
  334. {
  335.  
  336.    if ((bumm[nexpls] = malloc(sizeof(struct explstrc)+tmppos*2)) != NULL) {
  337.       sexpl = nexpls++;
  338.       expllen[sexpl] = sizeof(struct explstrc) + tmppos*2;
  339.       *bumm[sexpl] = tmpexpl;
  340.       memcpy(&bumm[sexpl]->data, tmp, tmppos*2);
  341.    }
  342. }
  343.  
  344.  
  345. void refreshpath(void)
  346. {
  347.    free(bumm[sexpl]);
  348.  
  349.    tmp[tmppos++] = EXPLEND;
  350.    if ((bumm[sexpl] = malloc(sizeof(struct explstrc)+tmppos*2)) != NULL) {
  351.       *bumm[sexpl] = tmpexpl;
  352.       memcpy(&bumm[sexpl]->data, tmp, tmppos*2);
  353.       expllen[sexpl] = sizeof(struct explstrc) + tmppos*2;
  354.    } else
  355.       error("CRITICAL OUT OF MEMORY. EXPLOSION LOST. DESIGNER UNSTABLE. QUIT!!!",0);
  356. }
  357.  
  358. void showpath(int x, int y, int path, int c)
  359. {
  360.    int  i;
  361.    int  c1, c2;
  362.  
  363.    i = 0;
  364.    x += sprite[foe[path]->sprite].sprite->xs/2;
  365.    y += sprite[foe[path]->sprite].sprite->ys/2;
  366.    plot(x, y, c);
  367.    c1 = foe[path]->path[i]; c2 = foe[path]->path[i+1];
  368.    while (((unsigned)c1 != FOEENDPATH) && ((unsigned)c1 != FOECYCLEPATH)) {
  369.  
  370.       switch (c1) {
  371.      case FOEMARK:
  372.         i += 1;
  373.         break;
  374.      case FOECHANGESPRITE:
  375.      case FOESOUND:
  376.         i += 2;
  377.         break;
  378.      case FOERELEASEFOE:
  379.         showpath(x+foe[path]->path[i+2], y+foe[path]->path[i+3], c2, (c == 0) ? 0 : c+1);
  380.         i += 4;
  381.         break;
  382.  
  383.      default:
  384.         plot(x+=c1, y+=c2, c);
  385.         i += 2;
  386.         break;
  387.  
  388.       }
  389.       c1 = foe[path]->path[i]; c2 = foe[path]->path[i+1];
  390.    }
  391.  
  392. }
  393.  
  394. int getpath(void)
  395. {
  396.    int   path0;
  397.    int   ch1, ch2;
  398.  
  399.    sfoe = 0;
  400.    setborder(-sprite[foe[sfoe]->sprite].sprite->xs, -sprite[foe[sfoe]->sprite].sprite->ys, 320, YMAX);
  401.    showpath(x, y, sfoe, 12);
  402.    putsprite(foe[sfoe]->sprite, x, y, 0);
  403.    do {
  404.       x0 = x; y0 = y; path0 = sfoe;
  405.       ch1 = ch2 = 0xff;
  406.       if(kbhit()) ch1 = getch();
  407.       if(ch1 == 0) ch2 = getch();
  408.       if (ch2 == 'A' || ch2 == 'B') sfoe = roll(sfoe, 0, nfoes-1, ch2-'A');
  409.       getpos(&b, &x, &y);
  410.       if ((x0 != x) || (y0 != y) || (sfoe != path0)) {
  411.      showpath(x0, y0, path0, 0);
  412.      removesprite(foe[path0]->sprite, x0, y0);
  413.      if (sfoe != path0) {
  414.         setborder(-sprite[foe[sfoe]->sprite].sprite->xs, -sprite[foe[sfoe]->sprite].sprite->ys, 320, YMAX);
  415.      }
  416.      showpath(x, y, sfoe, 11);
  417.      putsprite(foe[sfoe]->sprite, x, y, 0);
  418.      plot(MIDX, MIDY, 15);
  419.       }
  420.  
  421.    } while (!b);
  422.  
  423.    return b;
  424.  
  425. }
  426.  
  427. void explinsert(void)
  428. {
  429.    writetext(0, STATLINE-8, "1.SND,2.REMOVE,3.RELEASE,4.NEWPATH", font);
  430.  
  431.    switch (getch()) {
  432.  
  433.       case '1': // Sound a bit around.
  434.      insertcommand(findcommand(NULL, runpos, 1), 2, EXPLSOUND, getdec("SND."));
  435.       break;
  436.  
  437.       case '2': // Remove exploded object.
  438.      insertcommand(findcommand(NULL, runpos, 1), 1, EXPLREMOVEOBJ);
  439.      break;
  440.       case '3': // Release a foe.
  441.      if (nfoes <= 1) break;
  442.      removesprite(sspr, x, y);
  443.      b = getpath();
  444.      if (b == 1) { // Left mouse pressed
  445.         insertcommand(findcommand(NULL, runpos, 1), 4,
  446.               EXPLRELEASEFOE, sfoe, x-MIDX, y-MIDY);
  447.      }
  448.      break;
  449.       case '4': // Release a foe and move it to a position.
  450.      if (nfoes <= 1) break;
  451.      removesprite(sspr, x, y);
  452.      b = getpath();
  453.      if (b == 1) { // Left mouse pressed
  454.         insertcommand(findcommand(NULL, runpos, 1), 6,
  455.               EXPLNEWPATH, sfoe,
  456.               -sprite[foe[sfoe]->sprite].sprite->xs/2,
  457.               -sprite[foe[sfoe]->sprite].sprite->ys/2,
  458.               x, y);
  459.      }
  460.      foe[sfoe]->flags |= FOE_LINE;
  461.      break;
  462.    }
  463.  
  464. }
  465.  
  466. void deletecommand(int pos)
  467. {
  468.    int  i;
  469.  
  470.    memmove(&tmp[pos], &tmp[i = findcommand(NULL, pos, 1)], (tmppos-pos)*2);
  471.    i -= pos;
  472.    tmppos -= i;
  473.    refreshpath();
  474.  
  475. }
  476.  
  477. void delexpl(void)
  478. {
  479.    int  i;
  480.  
  481.    free(bumm[sexpl]);
  482.    for (i = sexpl + 1; i < nexpls; i++)
  483.       bumm[i-1] = bumm[i];
  484.    nexpls--;
  485.    if (nexpls == 0) {
  486.       tmppos = 1; tmp[0] = EXPLEND;
  487.       storepath();
  488.       tmppos = 0; fillwait();
  489.       runpos = -1;
  490.    } else {
  491.       sexpl--; if (sexpl < 0) sexpl = nexpls - 1;
  492.       newexpl();
  493.    }
  494. }
  495.  
  496.  
  497.  
  498. int getkey(void)
  499. {
  500.    int   ch1, ch2;
  501.  
  502.  
  503.    ch1 = ch2 = 0xff;
  504.    if(kbhit()) ch1 = getch();
  505.    if(ch1 == 0) ch2 = getch();
  506.  
  507.    if (ch1 != 0xff)
  508.    if (ch1 == 0) {
  509.  
  510.       switch (ch2) {
  511.  
  512.      case 'A': // F7 + F8
  513.      case 'B':
  514.            refreshpath();
  515.            sexpl = roll(sexpl, 0, nexpls - 1, ch2 - 'A');
  516.            count = 0;
  517.            newexpl();
  518.            break;
  519.  
  520.      case 'C': // F9
  521.      case 'D':
  522.            removesprite(sspr, x, y);  // F10
  523.            sspr = roll(sspr, 0, nsprites - 1, ch2 - 'C');
  524.            putsprite(sspr, x, y, 0);
  525.            break;
  526.  
  527.      case 'M': count++;  // Cursor right
  528.            runpos = findcommand(NULL, runpos, 1);
  529.            break;
  530.      case 'K': count--;  // cursor left
  531.            runpos = findcommand(NULL, runpos, -1);
  532.            break;
  533.  
  534.      case 'S': // Delete key
  535.            deletecommand(runpos);
  536.            break;
  537.      case 'R': // Insert key
  538.            explinsert();
  539.            break;
  540.  
  541.       }
  542.  
  543.       status();
  544.  
  545.    } else {
  546.  
  547.       switch (ch1) {
  548.  
  549.      case 'd': // Delete explosion
  550.            delexpl();
  551.                    count = 0;
  552.            break;
  553.      case 'n': // New Explosion
  554.            refreshpath();
  555.            count = 0;
  556.            tmppos = 1; tmp[0] = EXPLEND;
  557.            storepath();
  558.            tmppos = 0; fillwait();
  559.            runpos = -1;
  560.            break;
  561.  
  562.      case 'q': return TRUE;
  563.  
  564.       }
  565.  
  566.       status();
  567.  
  568.    }
  569.  
  570.    return FALSE;
  571.  
  572. }
  573.  
  574. int addexpl(void)
  575. {
  576.  
  577.    insertcommand(findcommand(NULL, runpos, 1), 4,
  578.          EXPLNEW, sspr, x-MIDX, y-MIDY);
  579.  
  580.    return 0;
  581.  
  582. }
  583.  
  584.  
  585. int doexpl(void)
  586. {
  587.    int   i;
  588.  
  589.    if (sprite == NULL) {
  590.       printf("\nSprite Library not loaded.\n");
  591.       return -1;
  592.    }
  593.  
  594.    initxmode();
  595.    initmouse();
  596.    setborder(0, 0, XMAX, YMAX);
  597.  
  598.    for (i = 0; i < nsprites; i++)
  599.       if (defsprite(sprite[i].sprite, sprite[i].flags) == -1) {
  600.      shutxmode();
  601.      printf("Not enough free offscreen memory.\n");
  602.      exit(1);
  603.       }
  604.  
  605.    count = 0;
  606.    if (nexpls > 0) {
  607.       sexpl = 0;
  608.       newexpl();
  609.    } else {
  610.       tmppos = 1; tmp[0] = EXPLEND;
  611.       storepath();
  612.       tmppos = 0; fillwait();
  613.       runpos = -1;
  614.    }
  615.  
  616.    status();
  617.    getpos(&b, &x, &y);
  618.    putsprite(sspr, x, y, 0);
  619.    plot(MIDX, MIDY, 15);
  620.  
  621.    do {
  622.       x0 = x; y0 = y;
  623.       getpos(&b, &x, &y);
  624.       if (b == 1) {
  625.      addexpl();
  626.      status();
  627.       }
  628.       if ((x0 != x) || (y0 != y)) {
  629.      removesprite(sspr, x0, y0);
  630.      putsprite(sspr, x, y, 0);
  631.      plot(MIDX, MIDY, 15);
  632.       }
  633.  
  634.    } while (!getkey());
  635.  
  636.    refreshpath();
  637.    killallsprites();
  638.  
  639.    clearmouse();
  640.    shutxmode();
  641.  
  642.    return 0;
  643. }
  644.  
  645.  
  646.  
  647. int loadexpls(char *file)
  648. {
  649.    char     fname[80];
  650.    int      filvar, filvar1;
  651.    int      nentries;
  652.    int      i;
  653.  
  654.    strcpy(fname, file);
  655.    if ((filvar = open(strcat(fname, ".EXP"), O_RDONLY | O_BINARY, S_IREAD)) == -1) {
  656.       return -1;
  657.    }
  658.    read(filvar, &nexpls, sizeof(nexpls));
  659.  
  660. // Read start points
  661.    strcpy(fname, file);
  662.    if ((filvar1 = open(strcat(fname, ".ESP"), O_RDONLY | O_BINARY, S_IREAD)) == -1) {
  663.       return -1;
  664.    }
  665.    read(filvar1, expllen, nexpls*2);
  666.    close(filvar1);
  667. // --
  668.  
  669.    read(filvar, bumm, nexpls*4);
  670.  
  671.    for (i = 0; i < nexpls; i++) {
  672.       if ((bumm[i] = malloc(expllen[i])) == NULL) {
  673.      close(filvar);
  674.      return -1;
  675.       }
  676.       read(filvar, bumm[i], expllen[i]);
  677.    }
  678.  
  679.    close(filvar);
  680.  
  681.    return 0;
  682. }
  683.  
  684.  
  685. int saveexpls(char *file)
  686. {
  687.    char      fname[80];
  688.    int       filvar, i;
  689.    long      ptr, ptr0;
  690.    long      fptr1, fptr2;
  691.    unsigned  size;
  692.  
  693.  
  694.    strcpy(fname, file);
  695.    filvar = open(strcat(fname, ".EXP"), O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, S_IWRITE);
  696.    if (filvar == -1) return -1;
  697.  
  698.    write(filvar, &nexpls, 2);
  699.    fptr1 = tell(filvar);
  700.    ptr = nexpls*4;
  701.    write(filvar, bumm, ptr);
  702.  
  703.    for (i = 0; i < nexpls; i++) {
  704.       write(filvar, bumm[i], expllen[i]);
  705.       fptr2 = tell(filvar);
  706.       lseek(filvar, fptr1, SEEK_SET);
  707.       ptr0 = (ptr + ((ptr & 0xfff0) << 12)) & 0xffff000fl;
  708.       write(filvar, &ptr0, 4);
  709.       ptr += expllen[i];
  710.       fptr1 = tell(filvar);
  711.       lseek(filvar, fptr2, SEEK_SET);
  712.  
  713.    }
  714.    close(filvar);
  715.  
  716.    strcpy(fname, file);
  717.    filvar = open(strcat(fname, ".ESP"), O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, S_IWRITE);
  718.    if (filvar == -1) return -1;
  719.    write(filvar, expllen, nexpls*2);
  720.    close(filvar);
  721.  
  722.    return 0;
  723. }
  724.  
  725.  
  726. int loadpathes(char *file)
  727. {
  728.    char     fname[80];
  729.    int      filvar, filvar1;
  730.    int      nentries;
  731.    int      i;
  732.  
  733.    strcpy(fname, file);
  734.    if ((filvar = open(strcat(fname, ".FOE"), O_RDONLY | O_BINARY, S_IREAD)) == -1) {
  735.       return -1;
  736.    }
  737.    read(filvar, &nfoes, sizeof(nfoes));
  738.  
  739. // Read start points
  740.    strcpy(fname, file);
  741.    if ((filvar1 = open(strcat(fname, ".FSP"), O_RDONLY | O_BINARY, S_IREAD)) == -1) {
  742.       return -1;
  743.    }
  744.    read(filvar1, xstart, nfoes*2);
  745.    read(filvar1, ystart, nfoes*2);
  746.    read(filvar1, pathlen, nfoes*2);
  747.    close(filvar1);
  748. // --
  749.  
  750.    read(filvar, foe, nfoes*4);
  751.  
  752.    for (i = 0; i < nfoes; i++) {
  753.       if ((foe[i] = malloc(pathlen[i])) == NULL) {
  754.      close(filvar);
  755.      return -1;
  756.       }
  757.       read(filvar, foe[i], pathlen[i]);
  758.    }
  759.    close(filvar);
  760.  
  761.    return 0;
  762. }
  763.  
  764.  
  765.  
  766.  
  767. int savepathes(char *file)
  768. {
  769.    char      fname[80];
  770.    int       filvar, i;
  771.    long      ptr, ptr0;
  772.    long      fptr1, fptr2;
  773.    unsigned  size;
  774.  
  775.  
  776.    strcpy(fname, file);
  777.    filvar = open(strcat(fname, ".FOE"), O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, S_IWRITE);
  778.    if (filvar == -1) return -1;
  779.  
  780.    write(filvar, &nfoes, 2);
  781.    fptr1 = tell(filvar);
  782.    ptr = nfoes*4;
  783.    write(filvar, foe, ptr);
  784.  
  785.    for (i = 0; i < nfoes; i++) {
  786.       write(filvar, foe[i], pathlen[i]);
  787.       fptr2 = tell(filvar);
  788.       lseek(filvar, fptr1, SEEK_SET);
  789.       ptr0 = (ptr & 0xf) + ((ptr & 0xffff0) << 12);
  790.       write(filvar, &ptr0, 4);
  791.       ptr += pathlen[i];
  792.       fptr1 = tell(filvar);
  793.       lseek(filvar, fptr2, SEEK_SET);
  794.  
  795.    }
  796.    close(filvar);
  797.  
  798.    return 0;
  799. }
  800.  
  801. void shutsprites(void)
  802. {
  803.    killallsprites();
  804.    if (sprite != NULL) farfree(sprite);
  805. }
  806.  
  807.  
  808. int main(void)
  809. {
  810.    char   libfile[80];
  811.    int    choice;
  812.    int    i;
  813.  
  814.    screenmode(0x02);
  815.  
  816.    printf("EXPLOSION DESIGNER [c] Dec 1992 by Alpha-Helix.\n\n");
  817.  
  818.    if (loadfont() == -1) {
  819.       printf("Font file not found !\n");
  820.       abort();
  821.    }
  822.  
  823.    do {
  824.  
  825.    do {
  826.       printf("\n\n");
  827.       printf("1 - Edit Explosions.\n");
  828.       printf("6 - Load Level.\n");
  829.       printf("7 - Save Level.\n");
  830.       printf("8 - Read Sprite Library.\n");
  831.       printf("9 - Quit PE.\n");
  832.  
  833.       printf("\nYour choice -> "); choice = getche() - '0';
  834.       printf("\n\n");
  835.  
  836.       switch (choice) {
  837.  
  838.      case 1: doexpl();
  839.          break;
  840.  
  841.      case 6: printf("Level : ");
  842.          fflush(stdin);
  843.          scanf("%s", libfile);
  844.          if (loadpathes(libfile) == -1) {
  845.             perror("File not read: ");
  846.          }
  847.          if (loadexpls(libfile) == -1) {
  848.             perror("File not read: ");
  849.          } else {
  850.             sexpl = 0;
  851.          }
  852.          break;
  853.  
  854.      case 7: printf("Level : ");
  855.          fflush(stdin);
  856.          scanf("%s", libfile);
  857.          if (savepathes(libfile) == -1) {
  858.             perror("File not written: ");
  859.          }
  860.          if (saveexpls(libfile) == -1) {
  861.             perror("File not written: ");
  862.          }
  863.          break;
  864.  
  865.      case 8: printf("Sprite Library [.SLI]: ");
  866.          fflush(stdin);
  867.          scanf("%s", libfile);
  868.          strcat(libfile, ".SLI");
  869.          shutsprites();
  870.          readslib(libfile);
  871.          sspr = 0;
  872.          break;
  873.  
  874.  
  875.       }
  876.  
  877.    } while (choice != 9);
  878.  
  879.    printf("\n\nQuit without saving (Y/N)? "); choice = getche();
  880.    } while (choice != 'y');
  881.  
  882.    for (i = 0; i < nexpls; i++)
  883.       free(bumm[i]);
  884.    shutsprites();
  885.    free(font);
  886.    printf("\nThanks for using ED !\n\n\n\n");
  887.  
  888.    return 0;
  889.  
  890. }
  891.  
  892.  
  893.  
  894.